package com.poker.pokerprototype;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public int checkWin(String _idag, int[] card) {
		int win = 0;
		int v, s;
		value = new int[5];
		suit = new int[5];
		deuce_pos = new int[5];
		cardsuit = new int[5];
		cardvalue = new int[13];

		flush = false;
		straight = false;
		four_of_a_kind = false;
		four_aces = false;
		four_234 = false;
		three_of_a_kind = false;
		two_pair = false;
		tens_or_better = false;
		kings_or_better = false;
		jacks_or_better = false;
		joker = false;
		joker_pos = 0;
		ndeuce = 0;
		king_ace = false;
		one_pair = false;
		one_space = false;
		int st = 0;
		int sum = 0;
		for (int i = 0; i < 5; i++) {
			value[i] = (card[i]) % 13;
			suit[i] = (int) ((card[i]) / 13);
		}
		for (int i = 0; i < 4; i++) {
			deuce_pos[i] = 0;
			cardsuit[i] = 0;
		}
		for (int i = 0; i < 13; i++) {
			cardvalue[i] = 0;
		}
		for (int i = 0; i < 5; i++) {
			if (suit[i] == 5) {
				joker = true;
				joker_pos = i;
				value[i] = 0;
			}
			if (_idag == "dwild" && value[i] == 0) {
				ndeuce++;
				deuce_pos[ndeuce] = i;
			} else {
				v = value[i];
				cardvalue[v]++;
				s = suit[i];
				cardsuit[s]++;
			}
		}
		for (int i = 0; i < 4; i++) {
			if (cardsuit[i] == (5 - ndeuce) || cardsuit[i] == 4 && joker)
				flush = true;
		}

		if (_idag == "dpoker") {
			for (int i = 0; i < 13; i++) {
				if (cardvalue[i] == 1) {
					st++;
					sum += i;
				} else if (cardvalue[i] == 4) {
					four_of_a_kind = true;
				} else if (cardvalue[i] == 3) {
					three_of_a_kind = true;
				} else if (cardvalue[i] == 2) {
					if (i > 7)
						tens_or_better = true;
					if (one_pair)
						two_pair = true;
					else
						one_pair = true;
				} else if (st < 5) {
					st = 0;
				}
				if (st == 5) {
					straight = true;
				}
			}
			if (flush && straight && sum == 60)
				win = 1;
			else if (flush && straight)
				win = 2;
			else if (four_of_a_kind)
				win = 3;
			else if (three_of_a_kind && one_pair)
				win = 4;
			else if (flush)
				win = 5;
			else if (straight)
				win = 6;
			else if (three_of_a_kind)
				win = 7;
			else if (two_pair)
				win = 8;
			else if (tens_or_better)
				win = 9;
		} else if (_idag == "triple" || _idag == "dbonus") {
			for (int i = 2; i <= 14; i++) {
				if (cardvalue[i] == 1) {
					st++;
					sum += i;
				} else if (cardvalue[i] == 4) {
					if (i == 14)
						four_aces = true;
					else if (i < 5)
						four_234 = true;
					else
						four_of_a_kind = true;
				} else if (cardvalue[i] == 3) {
					three_of_a_kind = true;
				} else if (cardvalue[i] == 2) {
					if (i > 10)
						jacks_or_better = true;
					if (one_pair)
						two_pair = true;
					else
						one_pair = true;
				} else if (st < 5) {
					st = 0;
				}
				if (st == 5)
					straight = true;
			}
			if (flush && straight && sum == 60)
				win = 1;
			else if (flush && straight)
				win = 2;
			else if (four_aces)
				win = 3;
			else if (four_234)
				win = 4;
			else if (four_of_a_kind)
				win = 5;
			else if (three_of_a_kind && one_pair)
				win = 6;
			else if (flush)
				win = 7;
			else if (straight)
				win = 8;
			else if (three_of_a_kind)
				win = 9;
			else if (two_pair)
				win = 10;
			else if (jacks_or_better)
				win = 11;
		} else if (_idag == "jpoker") {
			for (int i = 2; i <= 14; i++) {
				if (cardvalue[i] == 4) {
					four_of_a_kind = true;
					st = 0;
				} else if (cardvalue[i] == 3) {
					three_of_a_kind = true;
					st = 0;
				} else if (cardvalue[i] == 2) {
					if (i >= 13)
						kings_or_better = true;
					if (one_pair)
						two_pair = true;
					else
						one_pair = true;
					st = 0;
				} else if (cardvalue[i] == 1) {
					st++;
					sum += i;
					if (i == 13 || i == 14)
						king_ace = true;
				} else {
					if (st > 0) {
						if (joker) {
							if (!one_space)
								one_space = true;
							else if (st < 4)
								st = 0;
						} else if (st < 5) {
							st = 0;
						}
					}
				}
			}
			if (st == 5 || st == 4 && joker)
				straight = true;
			if (flush && straight && sum == 60)
				win = 1;
			else if (four_of_a_kind && joker)
				win = 2;
			else if (flush && straight && joker && sum > 46)
				win = 3;
			else if (flush && straight && joker)
				win = 4;
			else if (four_of_a_kind || three_of_a_kind && joker)
				win = 5;
			else if (three_of_a_kind && one_pair || two_pair && joker)
				win = 6;
			else if (flush)
				win = 7;
			else if (straight)
				win = 8;
			else if (three_of_a_kind || one_pair && joker)
				win = 9;
			else if (two_pair)
				win = 10;
			else if (kings_or_better || king_ace && joker)
				win = 11;
		} else if (_idag == "dwild") {
			int nd = ndeuce;
			for (int i = 2; i <= 14; i++) {
				if (cardvalue[i] == 4) {
					four_of_a_kind = true;
					st = 0;
				} else if (cardvalue[i] == 3) {
					three_of_a_kind = true;
					st = 0;
				} else if (cardvalue[i] == 2) {
					if (one_pair)
						two_pair = true;
					else
						one_pair = true;
					st = 0;
				} else if (cardvalue[i] == 1) {
					st++;
					sum += i;
				} else {
					if (st > 0) {
						if (nd > 0)
							nd--;
						else if (st < (5 - ndeuce))
							st = 0;
					}
				}
			}
			if (st == (5 - ndeuce))
				straight = true;
			if (flush && straight && sum == 60)
				win = 1;
			else if (ndeuce == 4)
				win = 2;
			else if (flush
					&& straight
					&& (ndeuce == 3 && sum >= 21 || ndeuce == 2 && sum >= 33 || ndeuce == 1
							&& sum >= 46))
				win = 3;
			else if (four_of_a_kind && ndeuce == 1 || three_of_a_kind
					&& ndeuce == 2 || one_pair && ndeuce == 3)
				win = 4;
			else if (flush && straight)
				win = 5;
			else if (four_of_a_kind || three_of_a_kind && ndeuce == 1
					|| one_pair && ndeuce == 2 || ndeuce == 3)
				win = 6;
			else if (three_of_a_kind && one_pair || two_pair && ndeuce == 1)
				win = 7;
			else if (flush)
				win = 8;
			else if (straight)
				win = 9;
			else if (three_of_a_kind || one_pair && ndeuce == 1 || ndeuce == 2)
				win = 10;
		}
		return (win);
	}

	public void autoHold(String _idag, int win) {
		int v, s, p, k, m;
		k = 0;
		m = 0;

		for (int i = 0; i < 13; i++) {
			cardvalue[i] = 0;
		}
		if (win > 0) {
			for (int j = 0; j < 5; j++) {
				v = value[j];
				cardvalue[v]++;
				held[j] = true;
			}
			if (!flush && !straight && !(three_of_a_kind && one_pair)
					|| !one_pair && ndeuce == 3 || !two_pair && ndeuce == 1) {
				if (four_of_a_kind || four_aces || four_234 || three_of_a_kind
						&& ndeuce == 1 || one_pair && ndeuce == 2
						|| ndeuce == 3) {
					k = 0;
					for (int i = 0; i < 13; i++) {
						if (cardvalue[i] == 1) {
							k = i;
							break;
						}
					}
					v = 0;
					for (int j = 0; j < 5; j++) {
						v = value[j];
						if (v == k) {
							held[j] = false;
							break;
						}
					}
				} else if (three_of_a_kind || one_pair && joker || one_pair
						&& ndeuce == 1 || ndeuce == 2) {
					k = 0;
					for (int i = 0; i < 13; i++) {
						if (cardvalue[i] == 3 || cardvalue[i] == 2 && joker
								|| cardvalue[i] == 2 && ndeuce == 1
								|| ndeuce == 2) {
							k = i;
							break;
						}
					}
					v = 0;
					for (int j = 0; j < 5; j++) {
						v = value[j];
						if (v != k)
							held[j] = false;
					}
				} else if (two_pair) {
					one_pair = false;
					for (int i = 0; i < 13; i++) {
						if (cardvalue[i] == 2) {
							if (!one_pair) {
								k = i;
								one_pair = true;
							} else {
								m = i;
								break;
							}
						}
					}
					v = 0;
					for (int j = 0; j < 5; j++) {
						v = value[j];
						if (v != k) {
							if (v != m)
								held[j] = false;
						}
					}
				} else if (tens_or_better || jacks_or_better || kings_or_better
						|| king_ace && joker) {
					k = 0;
					for (int i = 0; i < 13; i++) {
						if (cardvalue[i] == 2) {
							k = i;
							break;
						} else if (i > 10 && cardvalue[i] == 1) {
							k = i;
						}
					}
					for (int j = 0; j < 5; j++) {
						v = value[j];
						if (v != k)
							held[j] = false;
					}
				}
			}
		} else {
			for (int j = 0; j < 5; j++) {
				held[j] = false;
			}
			brek = 0;
			for (int i = 0; i < 4; i++) {
				cardsuit[i] = 0;
			}
			for (int j = 0; j < 5; j++) {
				if (!(_idag == "dwild" && value[j] == 2)) {
					v = value[j];
					cardvalue[v]++;
					s = suit[j];
					cardsuit[s]++;
				}
			}
			for (int i = 0; i < 4; i++) {
				if (cardsuit[i] == 4 || cardsuit[i] == 3 && joker
						|| cardsuit[i] == 3 && ndeuce == 1) {
					for (int j = 0; j < 5; j++) {
						s = suit[j];
						if (s == i)
							held[j] = true;
					}
					brek = 1;
					break;
				}
			}
			int[] position = new int[13];
			int[] saveposition = new int[13];
			if (brek == 0) {
				for (int i = 0; i < 13; i++) {
					position[i] = 0;
				}
				for (int n = 0; n < 4; n++) {
					saveposition[n] = 0;
				}
				for (int j = 0; j < 5; j++) {
					v = value[j];
					position[v] = j;
				}
				int st = 0;
				one_space = false;
				for (int i = 0; i < 13; i++) {
					if (cardvalue[i] == 0) {
						if (st != 0) {
							if (!one_space) {
								one_space = true;
							} else {
								one_space = false;
								st = 0;
							}
						}
					} else if (cardvalue[i] == 1 || cardvalue[i] == 2) {
						st++;
						saveposition[st] = position[i];
					}
					if (st == 4 || st == 3 && joker || st == 3 && ndeuce == 1) {
						for (int n = 0; n < 4; n++) {
							p = saveposition[n];
							held[p] = true;
						}
						brek = 1;
						break;
					}
				}
			}
			if (brek == 0) {
				for (int i = 0; i < 13; i++) {
					if (cardvalue[i] == 2) {
						for (int j = 0; j < 5; j++) {
							if (value[j] == i)
								held[j] = true;
						}
						brek = 1;
						break;
					}
				}
			}
			if (brek == 0) {
				for (int j = 0; j < 5; j++) {
					if (_idag == "dpoker") {
						if (value[j] > 7)
							held[j] = true;
					} else if (_idag == "triple" || _idag == "dbonus") {
						if (value[j] > 8)
							held[j] = true;
					} else if (_idag == "jpoker") {
						if (value[j] > 10) {
							held[j] = true;
						}
					}
				}
			}
		}
		if (joker&&_idag == "jpoker")
			held[joker_pos] = true;
		for (int i = 0; i < ndeuce; i++) {
			held[deuce_pos[i]] = true;
		}

	}
   
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(shuffleBtn.equals(v)){
			shuffle();
			shuffleBtn.setText(card[0]+" "+card[1]+" "+card[2]+" "+card[3]+" "+card[4]+" win="+checkWin("dpoker", card));
		}
	}
}